﻿using System;
using System.Collections.Generic;
using Byond.Logging;
using System.Linq;

namespace Byond.Frontend
{
	class LogMessageCache
	{
		private readonly IUserLogger _logger;
		private readonly List<string> _logMessages = new List<string> { "Welcome!" };

		private void Ctor()
		{
			_logger.NewLogMessage += Log;
		}

		public event Action<string> NewLogMessages = _ => { };

		private void Log(string message)
		{
			lock (_logMessages)
			{
				_logMessages.Insert(0, message);
				if (_logMessages.Count > 500)
					_logMessages.RemoveAt(_logMessages.Count - 1);

				NewLogMessages(message);
			}
		}

		public IEnumerable<string> GetMessages()
		{
			lock (_logMessages)
				return _logMessages.ToList();
		}

		public void Clear()
		{
			lock (_logMessages)
				_logMessages.Clear();
		}
	}
}
